package com.example.zhuguoqing.ztinker

import android.Manifest
import android.app.ActivityManager
import android.content.Context
import android.os.Build
import android.os.Bundle
import android.os.Environment
import android.support.v7.app.AppCompatActivity
import com.tbruyelle.rxpermissions2.RxPermissions
import com.tencent.tinker.lib.tinker.TinkerInstaller
import io.reactivex.Observable
import kotlinx.android.synthetic.main.activity_main.*
import java.util.concurrent.TimeUnit


class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        RxPermissions(this)
                .request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .subscribe { granted ->
                    if (granted) {
//                        Observable.timer(5, TimeUnit.SECONDS).subscribe {
//                            if (isAppInBackground(this)) {
////                                TinkerInstaller.onReceiveUpgradePatch(application.applicationContext, Environment.getExternalStorageDirectory().absolutePath + "/ouyu.patch")//不要以.apk结尾，因为有些运营商会挟持以.apk结尾的资源
//                            }
//                        }
                    } else {
                    }
                }

        btTinker.setOnClickListener {
            Observable.timer(5, TimeUnit.SECONDS).subscribe {
                if (isAppInBackground(this)) {
//                                TinkerInstaller.onReceiveUpgradePatch(application.applicationContext, Environment.getExternalStorageDirectory().absolutePath + "/ouyu.patch")//不要以.apk结尾，因为有些运营商会挟持以.apk结尾的资源
//                                deleteFile(Environment.getExternalStorageDirectory().absolutePath + "/ouyu.patch")
                    TinkerInstaller.onReceiveUpgradePatch(application.applicationContext, Environment.getExternalStorageDirectory().absolutePath + "/ouyu.patch")//不要以.apk结尾，因为有些运营商会挟持以.apk结尾的资源
                }
            }
        }

    }

    /**
     * APP是否运行在后台
     *
     * @param context
     * @return
     */
    private fun isAppInBackground(context: Context): Boolean {
        var isInBackground = true
        val am = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) { // Android5.0及以后的检测方法
            val runningProcesses = am.runningAppProcesses
            for (processInfo in runningProcesses) {
                //前台程序
                if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                    for (activeProcess in processInfo.pkgList) {
                        if (activeProcess == context.packageName) {
                            isInBackground = false
                        }
                    }
                }
            }
        } else {
            val taskInfo = am.getRunningTasks(1)
            val componentInfo = taskInfo[0].topActivity
            if (componentInfo.packageName == context.packageName) {
                isInBackground = false
            }
        }
        return isInBackground
    }
}
